package com.study.pipe;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/28 8:49
 */
public class TestPipe
{
    public static void main(String[] args) throws IOException
    {
        Pipe pipe = Pipe.open();
        Pipe.SinkChannel sinkChannel = pipe.sink();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put("向管道发送数据".getBytes());
        buffer.flip();
        sinkChannel.write(buffer);

        Pipe.SourceChannel sourceChannel = pipe.source();
        buffer.flip();
        int i = sourceChannel.read(buffer);
        System.out.println(new String(buffer.array(),0,i));
        buffer.clear();
        sourceChannel.close();
        sinkChannel.close();
    }
}
